package com.iaz.tech.tools.ai.neuron.exec;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iaz.tech.tools.ai.neuron.runtime.INeuronRuntimeAbility;  
import com.iaz.tech.tools.message.IMessageAble;
import com.iaz.tech.tools.message.NeuronMessage;
import com.iaz.tech.tools.property.project.Project;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.behavioral.sequence.impl.NeuronSequence;
import com.iaz.tech.tools.uml.relationship.Relationship;
import com.iaz.tech.tools.uml.relationship.impl.NeuronMessageRelationship;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronSequenceCodeGenerator extends NeuronCodeGenerator{
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronSequenceCodeGenerator.class);
    
    public NeuronSequenceCodeGenerator() {
        super();
    }

    public NeuronSequenceCodeGenerator(INeuronRuntimeAbility runtime) {
        super();
        this.setRuntime(runtime);
    }
    
    @Override
    public Relationship toRelationship(IUMLNeuronAble uml) throws Exception {
        throw new IllegalArgumentException("uml必须为Class");
    }

    /**
     * 生成序列代码
     */
    @Override
    public String generateSourceCode(Project project, IUMLNeuronAble uml) {
        String result = "";
        try {
            if (uml == null) {
                throw new IllegalArgumentException("uml不能为null");
            }
            if (!(uml instanceof NeuronSequence)){
                throw new IllegalArgumentException("uml必须为NeuronSequence");
            }
            if (this.getRuntime() == null){
                throw new IllegalArgumentException("runtime不能为null");
            }
            if (project == null) {
                throw new IllegalArgumentException("project不能为null");
            }
            if (project.getProperties() == null) {
                throw new IllegalArgumentException("project.properties不能为null");
            }

            NeuronSequence sequence = (NeuronSequence) uml;
            if (sequence.getMessages() == null || sequence.getMessages().isEmpty()){
                throw new IllegalArgumentException("sequence.messages不能为null或空");
            }

            /* 消息队列 */
            Map<Integer, IMessageAble> messages = sequence.getMessages();
            if (messages == null || messages.isEmpty()){
                throw new IllegalArgumentException("sequence.messages不能为null或空");
            }

            for (Map.Entry<Integer, IMessageAble> entry : messages.entrySet()){
                if (!(entry.getValue() instanceof NeuronMessage)){
                    throw new IllegalArgumentException("sequence.messages中的元素必须为NeuronMessage");
                }
                if (!(entry.getValue() instanceof NeuronMessageRelationship)) {
                    logger.warn("sequence.messages中的元素{}不是NeuronMessageRelationship，将被忽略", entry.getKey());
                    continue;
                }
                NeuronMessageRelationship relationship = (NeuronMessageRelationship) entry.getValue();
                if (relationship.getCalled() == null){
                    logger.warn("sequence.messages中的元素{}的called不能为null或空", entry.getKey());
                    continue;
                }
                if (relationship.getDialing() == null){
                    logger.warn("sequence.messages中的元素{}的dialing不能为null或空", entry.getKey());
                    continue;
                }
                if (relationship.getMessage() == null || relationship.getMessage().isEmpty()){
                    logger.warn("sequence.messages中的元素{}的message不能为null或空", entry.getKey());
                    continue;
                }
                /* 用于存储消息 */    

            }
        } catch (Exception e) {
            logger.error("执行异常：" + e.getMessage());
        }
        return result;
    }

}
